package com.qd.panda.service.commodity;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdqidi.db.BaseService;
import com.cdqidi.dto.ApiResultWrapper;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.panda.mapper.commodity.CommodityStatisticsMapper;
import com.qd.panda.model.CommodityStatistics;
import com.qd.panda.service.third.dc.DataCenterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品统计
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class CommodityStatisticsService extends BaseService<CommodityStatisticsMapper, CommodityStatistics> {
    private final DataCenterService dataCenterService;

    @Transactional(rollbackFor = Exception.class)
    public void commodityStatistics() {
        QueryWrapper<CommodityStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sync_state", PandaConstant.SYNC_STATE_0);
        List<CommodityStatistics> list = baseMapper.selectList(queryWrapper);
        queryWrapper.clear();
        if (list.isEmpty()) {
            return;
        }
        ApiResultWrapper<String> resultWrapper = dataCenterService.commodityStatistics(list);
        if (resultWrapper.isSuccess()) {
            list.forEach(item -> item.setSyncState(PandaConstant.SYNC_STATE_1));
            baseMapper.updateById(list);
        }
        list.clear();
    }
}